<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">提货需求</h4>
</div>
<form id="apply-form" action="/xqc/shipping/sku-ajax" method="post" onsubmit="return checkApplyForm();">
    <div class="modal-body">
        <div class="box-body table-responsive no-padding" style="max-height: 550px;">
            <table class="table">
                <thead>
                <tr>
                    <th>周期</th>
                    <th>提货数量</th>
                    <th>编辑数量</th>
                </tr>
                </thead>
                <tbody>
                @foreach($re as $date => $item)
                    <tr>
                        <td>{{ $item['label'] }}半月</td>
                        <td>{{ $item['has_apply_num'] }}</td>
                        <td>
                            <input type="text" class="this_apply_num_"
                                   style="width: 100px; text-align: right;"
                                   name="param[detail][{{ $date }}][apply_num]"
                                   value="{{ $item['this_apply_num'] }}" required>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
    <div class="modal-footer">
        {{ csrf_field() }}
        <input type="hidden" name="action" value="doApply">
        <input type="hidden" name="param[id]" value="{{ $id }}">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="submit" class="btn btn-primary">确认</button>
    </div>
</form>
<script language="JavaScript" type="text/javascript">
    function checkApplyForm(){
        var error = false;
        $('.this_apply_num_').each(function(index){
            if($(this).val() < 0){
                error = true;
            }
        });
        if(error) {
            swal('提货数量不能为负', '', 'error');
            return false;
        }
        return true;
    }

    $(function () {
        $(".this_apply_num_").inputmask("integer",
            {
                onKeyValidation: function () {
                    if($(this).val() < 0){
                        $(this).val(0);
                    }
                }
            }
        );

        $("#apply-form").ajaxForm(function (data) {
            if (typeof data === 'object') {
                if (data.status) {
                    $("#tempModal").modal('hide');
                    $(".modal-backdrop").remove();
                    $('body').removeClass('modal-open');
                    toastr.success('提交成功 !');
                    $.pjax.reload('#pjax-container');
                } else {
                    swal(data.message, '', 'error');
                }
            }
        });
    });
</script>
